﻿using Aribaccio.Device;
using BDPAutodetect.Device;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BDPAutodetect.Simcore
{
    public class DeviceCommandManager
    {
        /// <summary>
        /// 程序集路径
        /// </summary>
        protected string AssemblyPath { get; set; }

        /// <summary>
        /// 存储文件夹路径
        /// </summary>
        protected string FolderPath => Path.Combine(this.AssemblyPath, "DataItems");

        /// <summary>
        /// 构造函数
        /// </summary>
        public DeviceCommandManager()
        {
            FileInfo finfo = new FileInfo(Assembly.GetExecutingAssembly().Location);
            this.AssemblyPath = finfo.DirectoryName ?? string.Empty;
        }

        /// <summary>
        /// 获取设备指令数据
        /// </summary>
        /// <param name="argDeviceName"></param>
        /// <returns></returns>
        public List<DeviceCommandData> GetDeviceCommandDatas(string argDeviceName)
        {
            try
            {
                string strFileName = $"{argDeviceName}.json";
                string strFilePath = Path.Combine(FolderPath, strFileName);
                string strJson = File.ReadAllText(strFilePath);
                DeviceCommandList? commandModels = JsonConvert.DeserializeObject<DeviceCommandList>(strJson);
                return commandModels != null && commandModels.CommandOptions != null
                    ? commandModels.CommandOptions
                    : new List<DeviceCommandData>();
            }
            catch (Exception)
            {
                return new List<DeviceCommandData>();
            }
        }
    }
}
